% 
% @(#)phoney_OutStreamX	1.2  3/16/88
%
export _OutStreamObject to "Builtins"

const _OutStreamObject == immutable object _OutStreamObject
  export getSignature, create

  const OutStreamType == type OutStreamType
    operation putChar [Character]
    operation putInt [Integer, Integer]
    operation putReal [ Real ]
    operation putString [ String ]
    operation flush
    operation close
  end OutStreamType

  function getSignature -> [ r : Signature ]
    r <- OutStreamType
  end getSignature

  operation create [ fd : Integer ] -> [r : OutStreamType]
    r <- object aUnixOutStreamType
      export putChar, putInt, putReal, putString, flush, close
      const myfd : Integer == fd
      monitor
	var isClosed : Boolean <- false

	operation putChar [r : Character]
	  if isClosed then returnAndFail end if
	  primitive "UnixStreamPutChar" [] <- [myfd, r]
	end putChar

	operation putInt [number : Integer, width : Integer]
	  if isClosed then returnAndFail end if
	  primitive "UnixStreamPutInt" [] <- [myfd, number, width]
	end putInt
	operation putReal [r : Real]
	  if isClosed then returnAndFail end if
	  primitive "UnixStreamPutReal" [] <- [myfd, r]
	end putReal
	operation putString [r : String]
	  if isClosed then returnAndFail end if
	  primitive "UnixStreamPutString" [] <- [myfd, r]
	end putString
	operation flush
	  if isClosed then returnAndFail end if
	  primitive "UnixStreamFlush" [] <- [myfd]
	end flush
	operation close
	  if isClosed then returnAndFail end if
	  isClosed <- true
	end close
      end monitor
    end aUnixOutStreamType
  end create
end _OutStreamObject
